package com.carrent.sys.controller;

import com.carrent.sys.domain.User;
import com.carrent.sys.service.UserService;
import com.carrent.sys.utils.DataGridView;
import com.carrent.sys.utils.ResultObj;
import com.carrent.sys.utils.WebUtils;
import com.carrent.sys.vo.UserVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;


@RestController
@RequestMapping("user")
@Api(tags = "用户管理控制器")
public class UserController {

	@Autowired
	private UserService userService;
	/**
	 * 加载用户列表返回DataGridView
	 */
	@RequestMapping("loadAllUser")
	public DataGridView loadAllUser(UserVo userVo) {
		return this.userService.queryAllUser(userVo);
	}
	
	
	/**
	 * 添加用户
	 */
	@RequestMapping("addUser")
	public ResultObj addUser(UserVo userVo) {
		try {
			this.userService.addUser(userVo);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.ADD_ERROR;
		}
	}
	
	/**
	 * 修改用户
	 */
	@RequestMapping("updateUser")
	public ResultObj updateUser(UserVo userVo) {
		try {
			this.userService.updateUser(userVo);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}
	
	
	/**
	 * 删除用户
	 */
	@RequestMapping("deleteUser")
	public ResultObj deleteUser(UserVo userVo) {
		try {
			this.userService.deleteUser(userVo.getUserid());
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
	
	/**
	 * 批量删除用户
	 */
	@RequestMapping("deleteBatchUser")
	public ResultObj deleteBatchUser(UserVo userVo) {
		try {
			this.userService.deleteBatchUser(userVo.getIds());
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
	/**
	 * 重置用户密码
	 */
	@RequestMapping("resetUserPwd")
	public ResultObj resetUserPwd(UserVo userVo) {
		try {
			this.userService.resetUserPwd(userVo.getUserid());
			return ResultObj.RESET_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.RESET_ERROR;
		}
	}
	
	
	/**
	 * 加载用户管理的分配角色的数据
	 */
	@RequestMapping("initUserRole")
	public DataGridView initUserRole(UserVo userVo) {
		return this.userService.queryUserRole(userVo.getUserid());
	}
	
	/**
	 * 保存用户和角色的关系
	 */
	@RequestMapping("saveUserRole")
	public ResultObj saveUserRole(UserVo userVo) {
		try {
			this.userService.saveUserRole(userVo);
			return ResultObj.DISPATCH_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DISPATCH_ERROR;
		}
	}

	//修改个人资料
	@RequestMapping("updateUserProfile")
	public void  updateUserProfile(User user){
		try{
			this.userService.updateUser(user);
			//放到session
			WebUtils.getHttpSession().setAttribute("user", user);
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	//修改个人头像
	@PostMapping(value = "/userImg",consumes = "multipart/form-data")
	public HashMap<String,String> uploadFile(String userid, @RequestParam MultipartFile file){
		Integer userId = Integer.parseInt(userid);
		HashMap<String,String> hashMap=new HashMap<>();
		try {
			User s = userService.uploadUserImg(userId, file);
			hashMap.put("SUCCESS","修改成功");
			return hashMap;
		}catch (Exception e){
			e.printStackTrace();
			hashMap.put("FAIL","修改失败");
			return hashMap;
		}


	}

	//根据userid查询user对象
	@RequestMapping("queryUserByUserId")
	public User queryUserByUserId(Integer userid){
		User user = userService.queryUserByUserId(userid);
		System.err.println("user = " + user);
		return user;
	}


	//更新用户Session信息
	@GetMapping("uploadUserSession")
	@ResponseBody
	public HashMap<String, User> uploadUserSession(Integer userid){
		User user = userService.queryUserByUserId(userid);
		WebUtils.getHttpSession().setAttribute("user", user);
		HashMap<String,User> hashMap=new HashMap<>();
		hashMap.put("user",user);
		return hashMap;
	}

}
